package com.xujie.sportsmeeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xujie.sportsmeeting.entity.CompetitionEvent;
import com.xujie.sportsmeeting.entity.EventJudge;
import com.xujie.sportsmeeting.entity.Judge;
import com.xujie.sportsmeeting.mapper.EventJudgeMapper;
import com.xujie.sportsmeeting.service.EventJudgeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目裁判员关联服务实现
 */
@Service
@RequiredArgsConstructor
public class EventJudgeServiceImpl extends ServiceImpl<EventJudgeMapper, EventJudge> implements EventJudgeService {

    @Override
    @Transactional
    public boolean assignJudgesToEvent(Long eventId, List<Long> judgeIds) {
        if (eventId == null || judgeIds == null || judgeIds.isEmpty()) {
            return false;
        }

        // 先删除已存在的分配（避免重复分配）
        LambdaQueryWrapper<EventJudge> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(EventJudge::getEventId, eventId)
                    .in(EventJudge::getJudgeId, judgeIds);
        remove(deleteWrapper);

        // 创建新的分配记录
        List<EventJudge> eventJudges = judgeIds.stream()
                .map(judgeId -> {
                    EventJudge eventJudge = new EventJudge();
                    eventJudge.setEventId(eventId);
                    eventJudge.setJudgeId(judgeId);
                    eventJudge.setJudgeRole(1); // 默认为主裁判
                    eventJudge.setAssignTime(LocalDateTime.now());
                    return eventJudge;
                })
                .collect(Collectors.toList());

        return saveBatch(eventJudges);
    }

    @Override
    @Transactional
    public boolean removeJudgesFromEvent(Long eventId, List<Long> judgeIds) {
        if (eventId == null || judgeIds == null || judgeIds.isEmpty()) {
            return false;
        }

        LambdaQueryWrapper<EventJudge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EventJudge::getEventId, eventId)
               .in(EventJudge::getJudgeId, judgeIds);

        return remove(wrapper);
    }

    @Override
    public List<Judge> getJudgesByEventId(Long eventId) {
        if (eventId == null) {
            return List.of();
        }
        return baseMapper.selectJudgesByEventId(eventId);
    }

    @Override
    public List<CompetitionEvent> getEventsByJudgeId(Long judgeId) {
        if (judgeId == null) {
            return List.of();
        }
        return baseMapper.selectEventsByJudgeId(judgeId);
    }

    @Override
    public boolean isJudgeAssignedToEvent(Long eventId, Long judgeId) {
        if (eventId == null || judgeId == null) {
            return false;
        }

        LambdaQueryWrapper<EventJudge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EventJudge::getEventId, eventId)
               .eq(EventJudge::getJudgeId, judgeId);

        return count(wrapper) > 0;
    }
}
